{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基础类：Data  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* data.x (Tensor, optional) – 节点特征矩阵，[节点数量, 节点特征维度]. (default: None)\n",
    "* data.edge_index (LongTensor, optional) – 图邻接稀疏矩阵，[2, 边数量]. (default: None)\n",
    "* data.edge_attr (Tensor, optional) – 边特征矩阵 [边数量, 节点特征维度]. (default: None)\n",
    "* data.y (Tensor, optional) – 图或节点的标签. (default: None)\n",
    "* data.pos (Tensor, optional) – 节点位置矩阵 [节点个数, 维度]. (default: None)\n",
    "* data.normal (Tensor, optional) – 法向量矩阵 [节点个数, 维度]. (default: None)\n",
    "* data.face (LongTensor, optional) – 面邻接矩阵 [3, 面数量]. (default: None)\n",
    "\n",
    "### p.s.\n",
    "    Data对象并不局限于以上属性，可按照需求添加任何需要的数据\n",
    "    如：\n",
    "    data = Data(x=x, edge_index=edge_index)\n",
    "    data.train_idx = torch.tensor([...], dtype=torch.long)\n",
    "    data.test_mask = torch.tensor([...], dtype=torch.bool)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 样例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://pytorch-geometric.readthedocs.io/en/latest/_images/graph.svg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2021-09-03T08:43:06.481412Z",
     "start_time": "2021-09-03T08:43:06.466640Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Data(edge_index=[2, 6], relation_type=[4], x=[4, 1])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "from torch_geometric.data import Data\n",
    "\n",
    "# 定义节点邻接矩阵（稀疏矩阵），因为是无向图，每条边出现两次\n",
    "edge_index = torch.tensor([[0, 1],\n",
    "                           [1, 0],\n",
    "                           [1, 2],\n",
    "                           [2, 1],\n",
    "                           [1, 3],\n",
    "                           [3, 1]], dtype=torch.long)\n",
    "\n",
    "# 定义节点特征矩阵\n",
    "x = torch.tensor([[-1], [0], [1], [2]], dtype=torch.float)\n",
    "\n",
    "# 实例化Data对象\n",
    "data = Data(x=x, edge_index=edge_index.t().contiguous())\n",
    "\n",
    "# 添加边类别\n",
    "data.relation_type = torch.tensor([0,0,1,1],dtype=torch.long)\n",
    "\n",
    "# 加载到GPU\n",
    "device = torch.device('cuda')\n",
    "data = data.to(device)\n",
    "\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "song",
   "language": "python",
   "name": "song"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
